<template>
  <div class="factory-list-container">
    <header-search-form
      :searchFormData="searchFormData"
      @search="search"
    />
    <lan-table
      size="small"
      :config="dataTable"
      :tableData="tableData"
      @search="search"
    >
      <template #type="scope">
        <el-input v-model="scope.row.type"></el-input>
      </template>
    </lan-table>
  </div>
</template>

<script>
import * as moment from 'moment';
import HeaderSearchForm from './headerSearchForm';
import lanTable from '../../../components/lanTable';
import { SortType, MomentFormat } from '../../../constants/common';
import { parseError } from '../../../utils';
import { getFactoryListApi } from '../../../api/factory';
import { deleteFactoryApi } from '../../../api/factory';
import { Message, MessageBox } from 'element-ui';

export default {
  name: 'FactoryList',
  components: {
    lanTable,
    HeaderSearchForm
  },
  data() {
    return {
      searchFormData: {
        remoteLoading: '',
        areaUuid: '',
        name: ''
      },

      tableData: [],

      dataTable: {
        pageSize: 10,
        totalCount: 0,
        currentPage: 1,
        pageSizes: [5, 10, 20, 50, 100],
        sort: {
          sortType: SortType.DESC,
          orderBy: 'createdAt'
        },
        columnList: [
          {
            // name: 'index',
            label: '',
            type: 'index'
          },
          {
            name: 'name',
            label: '工厂名称'
          },
          {
            name: 'contactName',
            label: '联系人姓名'
          },
          {
            name: 'contactMobile',
            label: '联系人电话'
          },
          {
            name: 'areaName',
            label: '区域名称'
          },
          {
            name: 'location',
            label: '工厂地址',
            width: '400px'
          },
          {
            name: 'createdAt',
            label: '创建时间',
            sortable: true
          },
          {
            name: 'updatedAt',
            label: '更新时间'
          },
          {
            type: 'button',
            label: '操作',
            buttons: [
              {
                type: 'text',
                text: '编辑',
                size: 'small',
                method: row => {
                  this.showEdit(row);
                }
              },
              {
                type: 'text',
                text: '删除',
                size: 'small',
                method: row => {
                  this.remove(row.uuid, row.name);
                }
              }
            ]
          }
        ]
      }
    };
  },
  created() {
    this.search();
  },
  methods: {
    showEdit(row) {
      this.$router.push('/factory/edit/' + row.uuid);
    },
    search() {
      getFactoryListApi(
        this.dataTable.currentPage - 1,
        this.dataTable.pageSize,
        this.dataTable.sort.sortType,
        this.dataTable.sort.orderBy,
        this.searchFormData.areaUuid,
        this.searchFormData.name
      )
        .then(({ total_count, data }) => {
          this.dataTable.totalCount = total_count;
          this.tableData = data.map(factory => {
            return {
              uuid: factory.uuid,
              name: factory.name,
              contactName: factory.contact_name,
              contactMobile: factory.contact_mobile,
              createdAt: moment(factory.created_at).format(MomentFormat),
              updatedAt: moment(factory.updated_at).format(MomentFormat),
              areaName: factory.area.name,
              areaUuid: factory.area.uuid,
              location: factory.location
            };
          });
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => {});
    },

    remove(uuid, name) {
      MessageBox.confirm(`确定删除区域${name}吗？`, '删除提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(() => {
          deleteFactoryApi(uuid)
            .then(() => Message.success('删除工厂成功'))
            .catch(error => Message.error(parseError(error)))
            .finally(() => this.search());
        })
        .catch(() => {});
    },
    pageSizeChangeHandler() {
      this.currentPage = 1;
      this.search();
    },
    currentPageChangeHandler() {
      this.search();
    }
  }
};
</script>

<style lang="scss" scoped>
.factory-list-container {
  padding: 10px;
}
</style>
